﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>数据库表管理</title>
    <link rel="stylesheet" href="/static/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/static/assets/module/admin.css?v=318"/>
    <link rel="stylesheet" href="/static/assets/css/table.css"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-inline">
                    <label class="layui-form-label">表名称:</label>
                    <div class="layui-input-inline">
                        <input name="username" class="layui-input" placeholder="输入名称"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">表描述:</label>
                    <div class="layui-input-inline">
                        <input name="nickName" class="layui-input" placeholder="输入关键词"/>
                    </div>
                </div>
                <div class="layui-inline">&emsp;
                    <button class="layui-btn icon-btn" lay-filter="userTbSearch" lay-submit>
                        <i class="layui-icon">&#xe615;</i>搜索
                    </button>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 数据表格 -->
            <table id="tableTable" lay-filter="tableTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="userTbBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">预览</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="reset">生成代码</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="userEditDialog">
    <form id="userEditForm" lay-filter="userEditForm" class="layui-form model-form">
        <input name="userId" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">账号:</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入账号" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">用户名:</label>
            <div class="layui-input-block">
                <input name="nickName" placeholder="请输入用户名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">性别:</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="男" title="男" checked/>
                <input type="radio" name="sex" value="女" title="女"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">手机号:</label>
            <div class="layui-input-block">
                <input name="phone" placeholder="请输入手机号" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色:</label>
            <div class="layui-input-block">
                <div id="userEditRoleSel"></div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="userEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<script type="text/html" id="toolbarOepration">
    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="import" ><i
            class="layui-icon">&#xe654;</i>导入</button>
    <button class="layui-btn layui-btn-sm" lay-event="gen" ><i
            class="layui-icon">&#xe654;</i>生成代码</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDel">
        <i class="layui-icon layui-icon-delete"></i>批量删除</button>
</script>
<!-- 选择数据源 -->
<script type="text/html" id="chooseDataSourceDialog">
    <table id="dataSourceTable" lay-filter="dataSourceTable" class="layui-hide"></table>
    <div class="btn-circle" id="chooseDataSourceBtn" style="position: absolute; bottom: 60px;" title="确认">
        <i class="layui-icon layui-icon-ok"></i>
    </div>
</script>


<!-- js部分 -->
<script type="text/javascript" src="/static/assets/libs/jquery/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="/static/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="/static/assets/js/common.js?v=318"></script>
<script type="text/javascript" src="/static/assets/js/ajaxUtil.js?v=318"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'xmSelect','formX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var xmSelect = layui.xmSelect;
        var formX = layui.formX;

        var mytbl;
        //.存储当前页数据集
        var pageData = [];
        //.存储已选择数据集，用普通变量存储也行
        layui.data('checked', null);
        //.渲染完成回调
        var myDone = function(res) {
            //.假设你的表格指定的 id="maintb"，找到框架渲染的表格
            var tbl = $('#tableTable').next('.layui-table-view');

            //.记下当前页数据，Ajax 请求的数据集，对应你后端返回的数据字段
            pageData = res.data;
            var len = pageData.length;
            //.遍历当前页数据，对比已选中项中的 id
            for (var i = 0; i < len; i++) {
                if (layui.data('checked', pageData[i]['tableId'])) {
                    //.选中它，目前版本没有任何与数据或表格 id 相关的标识，不太好搞，土办法选择它吧
                    tbl.find('table>tbody>tr').eq(i).find('td').eq(0).find('input[type=checkbox]').prop('checked', true);
                }
            }

            //table 中点击选择后会记录到 table.cache，没暴露出来，也不能 mytbl.renderForm('checkbox');
            //.暂时只能这样渲染表单
            form.render('checkbox');
        };

        //.监听选择，记录已选择项
        table.on('checkbox(tableTable)', function(obj) {
            //.全选或单选数据集不一样
            var data = obj.type == 'one' ? [obj.data] : pageData;
            //.遍历数据
            $.each(data, function(k, v) {
                //.假设你数据中 id 是唯一关键字
                if (obj.checked) {
                    //.增加已选中项
                    layui.data('checked', {
                        key: v.tableId, value: v
                    });
                } else {
                    //.删除
                    layui.data('checked', {
                        key: v.tableId, remove: true
                    });
                }
            });
        });

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#tableTable',
            url: '/table/',
            page: true,
            skin: 'line',
            toolbar: '#toolbarOepration',
            cellMinWidth: 100,
            cols: [
                [
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'tableName', title: '表名称', sort: true},
                {field: 'tableComment', title: '表描述', sort: true},
                {field: 'className', title: '实体类名', sort: true},
                {
                    field: 'createTime', title: '创建时间', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, sort: true
                },
                {title: '操作', toolbar: '#userTbBar', align: 'center', minWidth: 200}
            ]
            ],
            done:myDone
        });

        /* 表格搜索 */
        form.on('submit(userTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(tableTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showTableInfoEdit(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            } else if (obj.event === 'reset') { // 重置密码
                resetPsw(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(tableTable)', function (obj) {
            if (obj.event === 'import') { // 添加
                importTable();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('tableTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }else if (obj.event === 'gen') {
                var checkRows = table.checkStatus('tableTable');
                console.log(layui.data('checked'));
                console.log(checkRows);
                console.log(pageData);
                console.log(checkRows.data);
                genCode(obj.data);
            }
        });

        function importTable() {
            admin.open({
                type: 1,
                area: '650px',
                offset: '65px',
                title: '选择数据源',
                content: $('#chooseDataSourceDialog').html(),
                success: function (layero) {
                    // 渲染表格
                    var insTbDs = table.render({
                        elem: '#dataSourceTable',
                        url: '/gen/db/list',
                        page: true,
                        height: 400,
                        skin: 'line',
                        cellMinWidth: 100,
                        cols: [
                            [
                            {type: 'radio', title: '#'},
                            {field: 'url', title: '数据库URL', width: 200},
                            {field: 'username', title: '用户名'},
                            {field: 'password', title: '密码'},
                            {field: 'remark', title: '备注'}
                        ]
                        ],
                        done: function () {
                            $(layero).find('.layui-table-view').css('margin', '0');
                        }
                    });
                    // 发表评论
                    $('#chooseDataSourceBtn').click(function () {

                    });

                }
            });
        }
        function showTableInfoEdit(mData) {
            var layIndex = admin.open({
                title: (mData ? '修改' : '添加') + '生成表信息',
                url: '/page/table/edit',
                area: ['90%', '60%'],
                offset: '10',
                end: function () {
                    if (admin.getLayerData(layIndex, 'formOk')) {  // 判断表单操作成功标识
                        insTb.reload();  // 成功刷新表格
                    }
                },
                success: function (layero, dIndex) {
                    // 弹窗超出范围不出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    formX.val('tableEditForm',mData);
                    form.render();
                    /* 渲染表格 */
                    var insTb1 = table.render({
                        elem: '#tableColumnTable',
                        url: '/table/'+mData.tableId+'/column',
                        page: false,
                        skin: 'line',
                        height : 'full-250',
                        cols: [
                            [
                            {
                                title:'<input type="checkbox" name="layTableCheckbox" lay-skin="primary" lay-filter="layTableAllChoose">',
                                width:60,
                                templet: '<div><input type="checkbox" name="layTableCheckbox" lay-skin="primary"> {{d.LAY_INDEX}}' +
                                    '<input type="hidden" name="columns[{{d.LAY_TABLE_INDEX}}].columnId" value="{{d.columnId}}"></div>'
                            },
                            {field: 'columnName', title: '字段列名'},
                            {field: 'columnComment', title: '字段描述',templet:'#columnCommentTb'},
                            {field: 'columnType', title: 'DB类型', sort: true},
                            {field: 'javaType', title: 'Java类型',templet: '#javaTypeSelect'},
                            {field: 'javaField', title: 'Java属性',templet: '#javaFieldTb'},
                            {field: 'queryType', title: '查询方式',templet: '#queryTypeSelect'},
                            {field: 'showType', title: '显示类型',templet: '#htmlTypeSelect'},
                            {field: 'isInsert', title: '插入', templet: '#functionInsertTb',width:58},
                            {field: 'isEdit', title: '编辑', templet: '#functionEditTb',width:58},
                            {field: 'isList', title: '列表', templet: '#functionListTb',width:58},
                            {field: 'isQuery', title: '查询', templet: '#functionQueryTb',width:58},
                            {field: 'isRequire', title: '必填', templet: '#functionRequireTb',width:58}
                            /*,
                            {
                                field: 'createTime', title: '创建时间', templet: function (d) {
                                    return util.toDateString(d.createTime);
                                }, sort: true
                            }*/
                        ]
                        ]
                    });

                    // 表单提交事件
                    form.on('submit(tableEditSubmit)', function (data) {
                        B.put({
                            url:'/table/',
                            data:$('#tableEditForm').serializeArray(),
                            dataType: 'json',
                            success:function (res) {
                                layer.close(layIndex);
                                if (res.code === 0) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    insTb.reload({page: {curr: 1}});
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }
        /* 显示表单弹窗 */
        function showEditModel1(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '用户',
                content: $('#userEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('userEditForm', mData);
                    // 表单提交事件
                    form.on('submit(userEditSubmit)', function (data) {
                        data.field.roleIds = insRoleSel.getValue('valueStr');
                        var loadIndex = layer.load(2);
                        $.get(mData ? '../../json/ok.json' : '../../json/ok.json', data.field, function (res) {  // 实际项目这里url可以是mData?'user/update':'user/add'
                            layer.close(loadIndex);
                            if (res.code === 200) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                    // 渲染多选下拉框
                    var insRoleSel = xmSelect.render({
                        el: '#userEditRoleSel',
                        name: 'userEditRoleSel',
                        layVerify: 'required',
                        layVerType: 'tips',
                        data: [{
                            name: '管理员',
                            value: 1
                        }, {
                            name: '普通用户',
                            value: 2
                        }, {
                            name: '游客',
                            value: 3
                        }]
                    });
                    // 回显选中角色
                    if (mData && mData.roles) {
                        insRoleSel.setValue(mData.roles.map(function (item) {
                            return item.roleId;
                        }));
                    }
                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        function genCode(data) {

        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                B.del({
                    url:'/table/'+obj.data.tableId,
                    success:function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({page: {curr: 1}});
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }
                });
            });
        }

    });
</script>
</body>
</html>
